package com.tmc.dao.impl.inthotel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.tmc.bean.common.Pagination;
import com.tmc.bean.inthotel.hotel.Country;
import com.tmc.dao.impl.BaseDaoImpl;
import com.tmc.util.ObjectUtil;

@SuppressWarnings("rawtypes")
@Repository
public class IntHotelCountryDaoImpl extends BaseDaoImpl {

	public List<Country> findCountryByCountryName(String countryName,String countryNameCN){
		Session session = getSession();
		String hql = "from Country c where c.countryName = :countryName or c.countryNameCN = :countryNameCN";
		Query query = session.createQuery(hql);
		query.setString("countryName",countryName);
		query.setString("countryNameCN",countryNameCN);
		List<Country> countries =(List<Country>)query.list();
		session.close();
		return countries;
	}
	
	public void saveAllCountry(List<Country> countries){
		Session session = getSession();
    	session.beginTransaction();
		for (int i = 0; i < countries.size(); i++) {
			session.save(countries.get(i));
		}
		session.getTransaction().commit();
	}
	
	@SuppressWarnings("unchecked")
	public List<Country> findAll(){
		Session session = getSession();
		String hql = "from Country c where c.enabledFlag = 'Y' and c.switchs = 0 ";
		Query query = session.createQuery(hql);
		List<Country> countries =(List<Country>)query.list();
		session.close();
		return countries;
	}
	
	@SuppressWarnings("unchecked")
	public List<Country> findAllCountry(){
		Session session = getSession();
		String hql = "from Country c where c.enabledFlag = 'Y' ";
		Query query = session.createQuery(hql);
		List<Country> countries =(List<Country>)query.list();
		session.close();
		return countries;
	}
	
	public void updateCountry(Country country){
		Session session = getSession();
    	session.beginTransaction();
		session.update(country);
		session.getTransaction().commit();
		session.close();
	}
	
	public void updateAllCountry(List<Country> countries){
		Session session = getSession();
    	session.beginTransaction();
		for (int i = 0; i < countries.size(); i++) {
			session.update(countries.get(i));
		}
		session.getTransaction().commit();
	}
	
	@SuppressWarnings("unchecked")
	public Pagination<Country> findByCon(Country country,Integer pageIndex, Integer pageSize){
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from Country c where c.enabledFlag = 'Y' and c.switchs = 0 ");
		if(ObjectUtil.hasText(country.getCountryName())){
			Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
    		Matcher m = p.matcher(country.getCountryName());
    		if(m.find()){
    			sql.append(" and c.countryNameCN like :countryName ");
    			params.put("countryName", "%" + country.getCountryName() + "%");
    		}else{
    			sql.append(" and c.countryName like :countryName ");
    			params.put("countryName", "%" + country.getCountryName() + "%");
    		}
		}
		
		sql.append(" order by c.countryId desc ");
		return this.findPagination(sql.toString(),params, pageIndex, pageSize);
	}
	
	@SuppressWarnings("unchecked")
	public Pagination<Country> findByCon1(Country country,Integer pageIndex, Integer pageSize){
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from Country c where c.enabledFlag = 'Y' ");
		if(ObjectUtil.hasText(country.getCountryName())){
			Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
    		Matcher m = p.matcher(country.getCountryName());
    		if(m.find()){
    			sql.append(" and c.countryNameCN like :countryName ");
    			params.put("countryName", "%" + country.getCountryName() + "%");
    		}else{
    			sql.append(" and c.countryName like :countryName ");
    			params.put("countryName", "%" + country.getCountryName() + "%");
    		}
		}
		
		sql.append(" order by c.countryId desc ");
		return this.findPagination(sql.toString(),params, pageIndex, pageSize);
	}
	
}
